{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <button id="btnAddUser" class="layui-btn icon-btn {:auth('mall.integral_item/add')}"><i class="layui-icon">
                &#xe654;</i>添加
            </button>
            <table id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="options">
    <a class="layui-btn layui-btn-sm layui-btn-xs {:auth('system.word/get_edit')}" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs {:auth('system.word/delete')}" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="thumb">
    <a href="{{d.thumb}}" target="_blank" title="点击查看">
        <img src="{{d.thumb}}" alt="" width="28" height="28">
    </a>
</script>
<script type="text/html" id="tableStateUser">
    <input type="checkbox" lay-filter="ckStateUser" value="{{d.id}}" lay-skin="switch"
           lay-text="启用|禁用" {{d.status==1?'checked':''}}/>
</script>
<script type="text/html" id="category">
    {{ d.admin_id.username }}
</script>
<!-- 表单弹窗 -->


<!-- js部分 -->
{include file='public/footer' /}
<!--<script type="text/javascript" src="/static/public/assets/js/common.js?v=315"></script>-->
<script>
    layui.use(['layer', 'table', 'admin', 'form'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var table = layui.table;
        //用户表格初始化
        var dataTable =
                table.render({
                    elem: '#tableUser'
                    , autoSort: false
                    , height: 500
                    , url: "{:__url('system.word/getData')}" //数据接口
                    , page: true //开启分页
                    , cols: [[ //表头
                        {checkbox: true, fixed: true}
                        , {field: 'id', title: 'ID', sort: true, width: 80}
                        , {field: 'content', title: '敏感词'}
                        , {field: 'number', sort: true,title: '命中次数'}
                        , {field: 'user', title: '提交人', toolbar: '#category'}
                        , {field: 'create_time', title: '创建时间'}
                        ,{field: 'status', sort: true, templet: '#tableStateUser', title: '操作'}
                        , {fixed: 'right', width: 260, align: 'center', toolbar: '#options'}
                    ]]
                });


        //监听工具条
        table.on('tool(tableUser)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                layer.confirm('确认删除吗？', function (index) {
                    layer.close(index);
                    var load = layer.load();
                    $.post("delete", {
                        _method: 'delete',
                        id: [data.id]
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1}, function () {
                                obj.del();
                            }, 1000)
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                showEditModel('编辑分类', '/system.word/edit?id=' + data.id);

            }
        });
        form.on('switch(ckStateUser)', function (obj) {
            msg = obj.elem.checked ?  '启用敏感词后拦截包含该词的内容,' +
            '确定要启用吗？' : '禁用敏感词后将无法拦截包含该敏感词的内容,' +
            '   确定要禁用吗？';

            layer.confirm(msg, {icon: 3, title:'提示'}, function(index){
                layer.load(2);
                $.post('{:url("action")}', {
                    id: obj.elem.value,
                    status: obj.elem.checked ? 1 : 0
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 0) {
                        layer.msg(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                }, 'json');
            });


        });
        // 添加按钮点击事件
        $('#btnAddUser').click(function () {
            showEditModel('新增商品', "add");
        });

        // 显示表单弹窗
        function showEditModel(title, url) {
            layer.full(admin.open({
                type: 2,
                title: title,
                content: url,
                btn:['提交','取消'],
                btnAlign: 'c',   // 按钮居中
                success: function (layero, dIndex) {
                    form.render();    // 表单渲染
                },

                yes: function (index, layero) {

                    // 获取弹出层中的form表单元素
                    var formSubmit = layer.getChildFrame('form', index);
                    //获取表单数据
                    var data = {dosubmit: 1};
                    var action = formSubmit[0]['action'];
                    var a = formSubmit.serializeArray();
                    $.each(a, function () {
                        if (data[this.name] !== undefined) {
                            if (!data[this.name].push) {
                                data[this.name] = [data[this.name]];
                            }
                            data[this.name].push(this.value || '');
                        } else {
                            data[this.name] = this.value || '';
                        }
                    });

                    if (data.parent_id == '') {
                        data.parent_id = '0';
                    }

                    layer.load(2);
                    $.post(action, data, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);

                            dataTable.reload({page: {curr: 1}});

                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');

                    return false;

                },
                btn2: function (index, layero) {
                    layer.close(index);
                }


            }));
        }
    })
</script>

</body>
</html>
